package com.man.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.man.entity.User;

public class UserDao {

	static String URL="jdbc:mysql://localhost:3306/manmanage?user=root&password=root";
	public boolean insert(User user  )
	{		Boolean b=false;
		String sql ="insert into user(userName,password,name,sex,birth,nation,edu,work,phone,place,email) values(?,?,?,?,?,?,?,?,?,?,?)";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, user.getUserName());
			statement.setString(2, user.getPassword());
			statement.setString(3, user.getName());
			statement.setString(4, user.getSex());
			statement.setString(5, user.getBirth());
			statement.setString(6, user.getNation());
			statement.setString(7, user.getEdu());
			statement.setString(8, user.getWork());
			statement.setString(9, user.getPhone());
			statement.setString(10, user.getPlace());
			statement.setString(11, user.getEmail());
		    statement.execute();
		    b=true;	
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return b;
		
	}
	public Boolean userSign(User user) 
	{
			Boolean b=false;
		String sql="select * from user where userName=? and password=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,user.getUserName());
			statement.setString(2,user.getPassword());
			ResultSet set=	statement.executeQuery();
			while(set.next())
			{
				b=true;
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
		
	}
	public User oneSelect(User user) 
	{	User user2=new User();
		String sql="select * from user where userName=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,user.getUserName());
			ResultSet set=	statement.executeQuery();
			while(set.next())
			{
				user2.setName(set.getString("name"));
				user2.setSex(set.getString("sex"));
				user2.setBirth(set.getString("birth"));
				user2.setNation(set.getString("nation"));
				user2.setEdu(set.getString("edu"));
				user2.setWork(set.getString("work"));
				user2.setPhone(set.getString("phone"));
				user2.setPlace(set.getString("place"));
				user2.setEmail(set.getString("email"));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user2;
		
	}
	public boolean oneUpdata(User user) 
	{	Boolean b=false;
		String sql="update user set name=?,sex=?,birth=?,nation=?,edu=?,work=?,phone=?,place=?,email=? where userName=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, user.getName());
			statement.setString(2, user.getSex());
			statement.setString(3, user.getBirth());
			statement.setString(4, user.getNation());
			statement.setString(5, user.getEdu());
			statement.setString(6, user.getWork());
			statement.setString(7, user.getPhone());
			statement.setString(8, user.getPlace());
			statement.setString(9, user.getEmail());
			statement.setString(10, user.getUserName());
			statement.execute();
			b=true;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
		
	}
	public boolean passwordUpdata(User user) 
	{
		
		Boolean b=false;
		String sql="update user set password=? where userName=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, user.getPassword());
			statement.setString(2, user.getUserName());
			statement.execute();
			b=true;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}
}
